package com.financeiro.util.jpa;

import java.io.Serializable;
import javax.inject.Inject;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

@Interceptor
@Transactional
public class TransactionInterceptor implements Serializable {

    private static final long serialVersionUID = 1L;
    private @Inject
    EntityManager manager;

    public Object invoke(InvocationContext context) throws Exception {
        EntityTransaction transaction = manager.getTransaction();

        boolean owner = false;

        try {
            if (!transaction.isActive()) {
                // truque para fazer rollback no que já passou
                // (senão, um futuro commit, confirmaria até mesmo operações sem transação)
                transaction.begin();
                transaction.rollback();

                // agora sim inicia a transação
                transaction.begin();

                owner = true;
            }
            return context.proceed();
        } catch (Exception e) {
            if (transaction != null && owner) {
                transaction.rollback();
            }
            throw e;
        } finally {
            if (transaction != null && transaction.isActive() && owner) {
                transaction.commit();
            }
        }
    }

}
